﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;
using System.Globalization;
namespace QA_System
{
    public class STRINGCLASS
    {
        static private string vnTagger = QA_System.Properties.Settings.Default.vnTaggerAbsolutePath;
        static private string xmlDatabasePath = QA_System.Properties.Settings.Default.xmlDatabasePath;
        // thay tu bi phan tich sai thanh tu thay the, them dau cau.
        public static string fixword(string asktext)
        {

            string word1 = "bức tranh";
            string word2 = "Bức tranh";
            string word4 = "giá tiền";
            string word3 = "giá cả";
            asktext = asktext.Replace("?", "?>");
            asktext = asktext.Replace(".", ".>");
          
            if (asktext.IndexOf(word1) != -1) asktext = asktext.Replace(word1, "tranh");
            if (asktext.IndexOf(word2) != -1) asktext = asktext.Replace(word2, "tranh");
            if (asktext.IndexOf(word3) != -1) asktext = asktext.Replace(word3, "giá");
            if (asktext.IndexOf(word4) != -1) asktext = asktext.Replace(word4, "giá");

            return asktext;

        }

        // ghi cau hoi tu textbox ra file text de chuan bi cho viec tagger.
        // luu moi cau 1 dong moi cho phan tich dung
        public static void getSentences(string asktext, string outputFile)
        {
            using (StreamWriter output = new StreamWriter(outputFile))
            {
                string[] _sentence = asktext.Split('>');
                int i = 0;

                while (i < _sentence.Length)
                {
                    byte[] utf8String = Encoding.UTF8.GetBytes(_sentence[i]);
                    output.WriteLine(Encoding.UTF8.GetString(utf8String));
                    i = i + 1;
                    output.Flush();
                }

                output.Close();
            }
        }


        //tien hanh tagger file cau hoi da luu
        public static void tagger(string inputFile, string outputFile)
        {
            Process process = new Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.RedirectStandardOutput = false;
            process.StartInfo.RedirectStandardError = false;
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            //process.StartInfo.CreateNoWindow = false;
            process.StartInfo.Arguments = "/c "+ vnTagger +" -i "+ inputFile+ " -o " + outputFile;
            process.Start();
            process.WaitForExit();
            process.Close();
        }

        // goi bkparser
        public static void bkparser()
        {
            Process process = new Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.RedirectStandardOutput = false;
            process.StartInfo.RedirectStandardError = false;
            process.StartInfo.FileName = "cmd.exe";
            //process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            //process.StartInfo.CreateNoWindow = false;
            process.StartInfo.WorkingDirectory = "C:\\bkparser_desktop";
            //process.StartInfo.Arguments = "/c C:\\bkparser_desktop\\connect.bat";
            process.StartInfo.Arguments = "/c java -Dinput=text1.txt -Doutput=text1.xml -jar BKParser.jar";
            // text1.txt và text1.xml là file đầu vào nằm trong thư mục C:\\bkparser_desktop
            process.Start();
            process.WaitForExit();
            process.Close();

        }

        // doc file sau khi tagger
        public static List<sentence_tagger> readXmlFile2(string istrPath)
        {
            XmlDocument newdoc = new XmlDocument();
            newdoc.Load(istrPath);
            XmlNodeList nodelist = newdoc.SelectNodes("//s");
            int l = nodelist.Count;
            List<sentence_tagger> sentenceList = new List<sentence_tagger>(l);// so cau            
            foreach (XmlNode node in nodelist)
            {
                int j = 0;
                sentence_tagger sentence;
                sentence = new sentence_tagger(node.ChildNodes.Count);
                foreach (XmlNode child in node.ChildNodes)
                {
                    sentence[j].Word_pos = child.Attributes["pos"].Value;
                    sentence[j].Word_text = child.InnerText;
                    j++;
                }
                sentenceList.Add(PrepareStructure.FixSentence(sentence));                
            }
            return sentenceList;
        }
        public static sentence_tagger[] readXmlFile1(string istrPath)
        {
            XmlDocument newdoc = new XmlDocument();
            newdoc.Load(istrPath);
            XmlNodeList nodelist = newdoc.SelectNodes("//s");
            int l = nodelist.Count;

            sentence_tagger[] sentencelistmember = new sentence_tagger[l];// so cau

            int i = 0;


            foreach (XmlNode node in nodelist)
            {
                int j = 0;
                sentence_tagger sentencelist;
                sentencelist= new sentence_tagger(node.ChildNodes.Count);
                foreach (XmlNode child in node.ChildNodes)
                {
                    sentencelist[j].Word_pos = child.Attributes["pos"].Value;
                    sentencelist[j].Word_text = child.InnerText;
                    j++;

                }
                sentencelistmember[i] = sentencelist;
                i++;
            }
            return sentencelistmember;

        }

        public static sentence_tagger[] readXmlFile(string istrPath)
        {
            try
            {
                XmlDocument newdoc = new XmlDocument();
                newdoc.Load(istrPath);
                XmlNodeList nodelist = newdoc.SelectNodes("//s");
                if (nodelist != null)
                {
                    sentence_tagger finalSentence;
                    sentence_tagger[] sentencelistmember = new sentence_tagger[nodelist.Count];// so cau
                    sentence_tagger sentencelist;
                    int i = 0;
                    #region sentence optimiz
                    foreach (XmlNode node in nodelist)
                    {
                        int j = 0;
                        sentencelist = new sentence_tagger(node.ChildNodes.Count);
                        foreach (XmlNode child in node.ChildNodes)
                        {
                            if (child.InnerText.ToLower().Equals("bao nhieu"))
                            {
                                sentencelist[j].Word_pos = "X";
                                sentencelist[j].Word_text = child.InnerText;
                                j++;
                            }
                            else
                            {

                                if (j > 0)
                                {
                                    #region j>0
                                    if ((sentencelist[j - 1].Word_pos.Contains("N")) && (child.Attributes["pos"].Value.Contains("N")))
                                    {
                                        if (sentencelist[j - 1].Word_pos.Equals("Nc"))
                                        {
                                            sentencelist[j - 1].Word_text += " " + child.InnerText;
                                            sentencelist[j - 1].Word_pos = child.Attributes["pos"].Value;
                                        }
                                        else
                                        {

                                            string strWord = sentencelist[j - 1].Word_text + " " + child.InnerText;
                                            if (XMLNounConnection.IsCompositNoun(strWord, xmlDatabasePath + "NounXMLdatabase.xml"))
                                            {
                                                sentencelist[j - 1].Word_text = strWord;
                                            }
                                            else
                                            {
                                                sentencelist[j].Word_pos = child.Attributes["pos"].Value;
                                                sentencelist[j].Word_text = child.InnerText;
                                                j++;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if ((sentencelist[j - 1].Word_pos.Equals("P")) && (child.Attributes["pos"].Value.Equals("P")))
                                        {
                                            sentencelist[j - 1].Word_pos = "XX";
                                            sentencelist[j].Word_pos = "XX";
                                            sentencelist[j].Word_text = child.InnerText;
                                            j++;
                                        }
                                        else
                                        {
                                            if ((sentencelist[j - 1].Word_pos.Equals("V")) && (child.Attributes["pos"].Value.Equals("V")))
                                            {
                                                sentencelist[j - 1].Word_text += " " + child.InnerText;
                                            }
                                            else
                                            {
                                                if (child.InnerText != string.Empty)
                                                {
                                                    sentencelist[j].Word_pos = child.Attributes["pos"].Value;
                                                    sentencelist[j].Word_text = child.InnerText;
                                                    j++;
                                                }
                                            }
                                        }
                                    }
                                    #endregion
                                }
                                else
                                {
                                    if ((!string.IsNullOrEmpty(child.InnerText)) || (child.InnerText != " "))
                                    {
                                        sentencelist[j].Word_pos = child.Attributes["pos"].Value;
                                        sentencelist[j].Word_text = child.InnerText;
                                        j++;
                                    }
                                }
                            }
                        }
                        #region intinize...
                        finalSentence = new sentence_tagger(j);
                        for (int k = 0; k < j; k++) finalSentence[k] = sentencelist[k];
                        #endregion
                        sentencelistmember[i] = finalSentence;
                        i++;
                    }
                    #endregion
                    return sentencelistmember;
                }
                else return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /*
        // tim tu khoa la so tu
        public static string extract_keyword_M(sentence_tagger keyword)
        {
            string keyword_true = "";
            int total = keyword.totalword();
            for (int i = 2; i < total; i++)
                if (keyword[i].Word_pos == "M")
                    keyword_true += keyword[i].Word_text.ToString() ; 
                    //keyword_true += keyword[i].Word_text + " ";

            return keyword_true;
        }

        // tim tu khoa la tu chi truong phai hay chat lieu
        public static string extract_keyword_G_M(sentence_tagger keyword)
        {
            string keyword_true = "";
            string all_g_m = Genre.GetAllKindOfGenre() + Painting.GetAllMaterial();
            int total = keyword.totalword();
            string keyword_string = "";
            for (int j = 2; j < total; j++) keyword_string += keyword[j].Word_text + " ";
            string[] all_g_m_list = all_g_m.Split('/');
            bool stop = false; int i = 0;
            while (stop == false)
            {
                if (keyword_string.Contains(all_g_m_list[i])) { keyword_true = all_g_m_list[i]; stop = true; }
                else { i++; if (i == all_g_m_list.Length) stop = true; }
            }
            return keyword_true;
        }
        */
        // tim tu khoa la danh tu rieng
        //public static string clearWord(string keyword)
        //{
            
        //    string realKeyword = "";
        //    string[] wordArray = keyword.Split(' ');
            
        //}
        public static string extract_keyword_N(sentence_tagger keyword)
        {
            string keyword_true = "";
            string keyword_true_rev = "";
            int total = keyword.totalword();
            string keyword_string = "";
            string meanlessWord = "do/bị/được/là/coi/nên/của";
            for (int j = 2; j < total; j++) keyword_string += keyword[j].Word_text + " ";

            string[] keyword_string_array = keyword_string.Split(' ');
            bool stop = false; int i = keyword_string_array.Length - 2;


            // tim nguoc xau
            while (stop == false)
            {
                char[] charArr = keyword_string_array[i].ToCharArray();
                if ((charArr[0].ToString() == charArr[0].ToString().ToUpper()) && (!Char.IsDigit(charArr[0])))
                {
                    if (i == 0)
                    {
                        if (keyword_true != "")
                        {
                            if ((keyword_string_array[i] != "Tranh") && (keyword_string_array[i] != "Bức"))
                                keyword_true += keyword_string_array[i] + " ";
                        }
                        stop = true;
                    }
                    else
                    {
                        keyword_true += keyword_string_array[i] + " ";
                        i--;

                    }
                }
                else
                {
                    if (keyword_true == "")
                    {
                        i--;
                        if (i == -1) stop = true;
                    }
                    else
                    { stop = true; }

                }
            }

            // dao lai xau
            string[] keyword_true_array = keyword_true.Split(' ');
            for (int k = (keyword_true_array.Length - 2); k >= 0; k--)
            {
                if (k != 0) keyword_true_rev += keyword_true_array[k] + " ";
                else keyword_true_rev += keyword_true_array[k];
            }


            return keyword_true_rev;
        }
        
        

        // kiem tra xem nut XML co chua tu khong?
        public static bool is_content(string root_string, string word_compare)
        { 
            string[] root_mem = root_string.Split('/');
            bool content = false;
            int i = 0;
            while ((content == false) && (i < root_mem.Length))
            {
                if (root_mem[i].Equals(word_compare.ToLower())) content = true;
                i++;
            }
            return content;
        }

        // lay toan bo duong di cua Token tim duoc, nguoc tu nut cuoi (EAT)
        public static string get_path_node(XmlNode node)
        {
            string path = node.Attributes["id"].Value.ToString() + " ";
            while (node.ParentNode.Name != "begin")
            {
                path += node.ParentNode.Attributes["id"].Value.ToString() + " ";
                node = node.ParentNode;

            }
            return path;

        }

        
        // duyet cau hoi, tim ra EAT 
        
        
    }
}
